//
//  YFHomeZeroReusableView.m
//  QuanJieShopProject
//
//  Created by 杨帆 on 2018/3/28.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "YFHomeZeroReusableView.h"
#import "SDCycleScrollView.h"
#import "YFHomeBtn.h"

#import "YFHomeOneReusableView.h"
#import "YFButtonCategoryView.h"

#import "YFHomeGoodsView.h"
#import "YFHomeRollView.h"
#import "LSLHotView.h"

#import "LSLHeadlineView.h"
#import "QJShopDetailVC.h"
#import "YFShopRegisterVC.h"
@interface YFHomeZeroReusableView()<SDCycleScrollViewDelegate>
@property (nonatomic, strong) SDCycleScrollView *cycleView;
@property (nonatomic, strong) YFButtonCategoryView *categoryView;
@property (nonatomic, strong) UIImageView *bigImgView1;
@property (nonatomic, strong) UIImageView *bigImgView2;

@property (nonatomic, strong) YFHomeGoodsView *smallImgView1;
@property (nonatomic, strong) YFHomeGoodsView *smallImgView2;
@property (nonatomic, strong) YFHomeGoodsView *smallImgView3;
@property (nonatomic, strong) YFHomeGoodsView *smallImgView4;
@property (nonatomic, strong)NSMutableArray *bannerArr;
@property (nonatomic, strong)NSMutableArray *bigArr;
@property (nonatomic, strong)NSMutableArray *smallArr;

@end


@implementation YFHomeZeroReusableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = YFColor_Background;
//        [self makeView];
    }
    return self;
}

- (void)setHeaderWithBannerArray:(NSMutableArray *)bannerArray LoopArray:(NSMutableArray *)loopArray BigArray:(NSMutableArray *)bigArray smallArray:(NSMutableArray *)smallArry{
    self.bannerArr = bannerArray;
    self.bigArr = bigArray;
    self.smallArr = smallArry;
    CGFloat lineW = 0.5;
    
    // 网络加载图片的轮播器  420
    if (_cycleView == nil) {
         self.cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT * 420.f/1334.f) delegate:self placeholderImage:[UIImage imageNamed:@"HBanner"]];
        self.cycleView.delegate = self;
        self.cycleView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        self.cycleView.currentPageDotImage = [UIImage imageNamed:@"banner_dian"];
        self.cycleView.pageDotImage = [UIImage imageNamed:@"banner_kong"];
         [self addSubview:self.cycleView];
    }
   
    NSMutableArray *newBannerArr = [NSMutableArray new];
    for (HbannerModel *model in bannerArray) {
        NSString *imgUrl = [NSString stringWithFormat:@"%@%@", NET_BASEURL,model.ad_code];
        [newBannerArr addObject:imgUrl];
    }
    self.cycleView.imageURLStringsGroup = newBannerArr;
    self.cycleView.delegate = self;
    
    NSLog(@"maxY=%f, height=%f", CGRectGetMaxY(self.cycleView.frame), UISCREEN_HEIGHT * 420.f/1334.f);
    
    
    
    // 30 150 150 = 330
    /*
     NSArray *titlesArr = @[@"分类",@"大健康",@"美食",@"娱乐",@"服装",@"酒水",@"生鲜水果",@"丽人/美发",@"K歌",@"酒店"];
     NSArray *imgesArr = @[@"fl",@"jk",@"ms",@"yl",@"fz",@"js",@"sx",@"lr",@"kg",@"jd"];
     */
    NSArray *titlesArr = @[@"服装",@"健康",@"酒水",@"美食",@"母婴儿童",@"家电",@"日用百货",@"美妆",@"厨具",@"汽车用品"];
    NSArray *imgesArr = @[@"fz",@"jk",@"js",@"ms",@"myet",@"jd",@"rybh",@"mz",@"cj",@"qcyp"];
    YFButtonCategoryView *categoryView = [[YFButtonCategoryView alloc] initWithOrigin:CGPointMake(0, CGRectGetMaxY(self.cycleView.frame))];
    categoryView.backgroundColor = [UIColor whiteColor];
    categoryView.buttonClickBlock = ^(NSInteger ZeroTag) {
        if (self.headerBtnClickBlock) {
            self.headerBtnClickBlock(ZeroTag);
        }
    };
    [categoryView updateWithTitlesArr:titlesArr imgsArr:imgesArr];
    if (_categoryView == nil) {
        _categoryView = categoryView;
    }
    [self addSubview:_categoryView];
   
    // 横线  数字1
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(categoryView.frame), UISCREEN_WIDTH, lineW)];
    line.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    [self addSubview:line];
    
    // 快报  115
    CGFloat expressNewsViewH = 115.f/1334.f * UISCREEN_HEIGHT;
    UIView *expressNewsView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(line.frame), UISCREEN_WIDTH, 50)];
    expressNewsView.backgroundColor = [UIColor whiteColor];
    
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];
    imgView.image = [UIImage imageNamed:@"泉界快报"];
    [expressNewsView addSubview:imgView];
    
    
    LSLHeadlineView *headlineView = [[LSLHeadlineView alloc] init];
    headlineView.frame = CGRectMake(50, 10, UISCREEN_WIDTH, 30);
    headlineView.scrollDirectionPortrait = YES;
     headlineView.images = loopArray;
    [self addSubview:expressNewsView];
    [expressNewsView addSubview:headlineView];
    //  积分专区  200
    CGFloat registerUnderLineViewH = 200.f/1334.f * UISCREEN_HEIGHT;
    UIView *registerUnderLineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(expressNewsView.frame), UISCREEN_WIDTH, registerUnderLineViewH)];
    registerUnderLineView.backgroundColor = self.backgroundColor;
    [UIColor colorWithHexString:@"dddddd"];
    [self addSubview:registerUnderLineView];
    {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, registerUnderLineViewH*0.8)];
        btn.center = CGPointMake(registerUnderLineView.frame.size.width/2, registerUnderLineView.frame.size.height/2);
        [btn setImage:[UIImage imageNamed:@"积分专区.png"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(registerUnderLineClick) forControlEvents:UIControlEventTouchUpInside];
        [registerUnderLineView addSubview:btn];
        
    }
    
    // 特惠  数字UISCREEN_WIDTH/2
    UIView *preferentialView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(registerUnderLineView.frame), UISCREEN_WIDTH, UISCREEN_WIDTH/2)];
    preferentialView.backgroundColor = [UIColor blueColor];
    [self addSubview:preferentialView];
    
    {
        UIImageView *leftImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, preferentialView.frame.size.width/2, preferentialView.frame.size.height)];
        //        leftImgView.backgroundColor = [UIColor randomColor];
        [preferentialView addSubview:leftImgView];
        self.bigImgView1 = leftImgView;
        
        // 118 95
        CGFloat leftTopImgViewW = 116.f/750.f * UISCREEN_WIDTH;
        CGFloat leftTopImgViewH = leftTopImgViewW * 95.f/118.f;
        UIImageView *leftTopImgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, leftTopImgViewW, leftTopImgViewH)];
        leftTopImgView.image = [UIImage imageNamed:@"miaosha"];
        [preferentialView addSubview:leftTopImgView];
        
        UIImageView *rightImgView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftImgView.frame), 0, preferentialView.frame.size.width/2, preferentialView.frame.size.height)];
        //        rightImgView.backgroundColor = [UIColor randomColor];
        [preferentialView addSubview:rightImgView];
        self.bigImgView2 = rightImgView;
        
        //  260 60
        CGFloat rightTopImgViewW = 127.f/750.f * UISCREEN_WIDTH;
        CGFloat rightTopImgViewH = leftTopImgViewW * 60.f/260.f;
        UIImageView *rightTopImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,20, rightTopImgViewW, rightTopImgViewH)];
        rightTopImgView.image = [UIImage imageNamed:@"youxuan"];
        [rightImgView addSubview:rightTopImgView];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(rightImgView.mj_x, 0, lineW, rightImgView.mj_w)];
        line.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
        [preferentialView addSubview:line];
    }
    
    // 商品优惠  数字(UISCREEN_WIDTH)/4 * 1.7
    CGFloat goodsViewW = (UISCREEN_WIDTH)/4;
    CGFloat goodsViewH = goodsViewW ; //
    UIView *goodsView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(preferentialView.frame), UISCREEN_WIDTH, goodsViewH)];
    goodsView.backgroundColor = [UIColor blueColor];
    [self addSubview:goodsView];
    {
        YFHomeGoodsView *view1 = [[YFHomeGoodsView alloc] initWithFrame:CGRectMake(0, 0, goodsViewW, goodsViewH)];
        //        view1.backgroundColor = [UIColor randomColor];
        [goodsView addSubview:view1];
        self.smallImgView1 = view1;
        
        YFHomeGoodsView *view2 = [[YFHomeGoodsView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(view1.frame), 0, goodsViewW, goodsViewH)];
        //        view2.backgroundColor = [UIColor randomColor];
        [goodsView addSubview:view2];
        self.smallImgView2 = view2;
        
        YFHomeGoodsView *view3 = [[YFHomeGoodsView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(view2.frame), 0, goodsViewW, goodsViewH)];
        //        view3.backgroundColor = [UIColor randomColor];
        [goodsView addSubview:view3];
        self.smallImgView3 = view3;
        
        YFHomeGoodsView *view4 = [[YFHomeGoodsView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(view3.frame), 0, goodsViewW, goodsViewH)];
        //        view4.backgroundColor = [UIColor randomColor];
        [goodsView addSubview:view4];
        self.smallImgView4 = view4;
        
        UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, lineW)];
        line1.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
        [goodsView addSubview:line1];
        
        UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(goodsViewW, 0, lineW, goodsViewH)];
        line2.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
        [goodsView addSubview:line2];
        
        UIView *line3 = [[UIView alloc] initWithFrame:CGRectMake(goodsViewW*2, 0, lineW, goodsViewH)];
        line3.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
        [goodsView addSubview:line3];
        
        UIView *line4 = [[UIView alloc] initWithFrame:CGRectMake(goodsViewW*3, 0, lineW, goodsViewH)];
        line4.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
        [goodsView addSubview:line4];
    }
    
    
    
    if (bigArray.count == 2) {
        HomeBigSmallModel *model1 = [bigArray objectAtIndex:0];
        [self.bigImgView1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",NET_BASEURL, model1.ad_code]] placeholderImage:nil];
        self.bigImgView1.tag = 100;
         [self addGestureRecognizerTapWithImageView:self.bigImgView1];
        HomeBigSmallModel *model2 = [bigArray objectAtIndex:1];
        [self.bigImgView2  sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",NET_BASEURL, model2.ad_code]] placeholderImage:nil];
        self.bigImgView2.tag = 200;
        [self addGestureRecognizerTapWithImageView:self.bigImgView2];
    }
    if (smallArry.count == 4) {
        
          HomeBigSmallModel *model1 = [smallArry objectAtIndex:0];
        [self.smallImgView1.imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",NET_BASEURL, model1.ad_code]] placeholderImage:nil];
        self.smallImgView1.imgView.tag = 1000;
        [self addGestureRecognizerTapWithImageView: self.smallImgView1.imgView];
       
        HomeBigSmallModel *model2 = [smallArry objectAtIndex:1];
        [self.smallImgView2.imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",NET_BASEURL, model2.ad_code]] placeholderImage:nil];
        self.smallImgView2.imgView.tag = 2000;
        [self addGestureRecognizerTapWithImageView: self.smallImgView2.imgView];
        
        
        HomeBigSmallModel *model3 = [smallArry objectAtIndex:2];
        [self.smallImgView3.imgView  sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",NET_BASEURL, model3.ad_code]] placeholderImage:nil];
        self.smallImgView3.imgView.tag = 3000;
        [self addGestureRecognizerTapWithImageView: self.smallImgView3.imgView];
        
        
        HomeBigSmallModel *model4 = [smallArry objectAtIndex:3];
        [self.smallImgView4.imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",NET_BASEURL, model4.ad_code]] placeholderImage:nil];
        self.smallImgView4.imgView.tag = 4000;
        [self addGestureRecognizerTapWithImageView: self.smallImgView4.imgView];
    }
    
    
    //入驻
    CGFloat ADViewH = 200.f/1334.f * UISCREEN_HEIGHT;
    UIView *ADView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(goodsView.frame), UISCREEN_WIDTH, ADViewH)];
    ADView.backgroundColor = self.backgroundColor;
    
    [self addSubview:ADView];
    {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, ADViewH*0.8)];
        btn.center = CGPointMake(ADView.frame.size.width/2, ADView.frame.size.height/2);
        [btn setImage:[UIImage imageNamed:@"AD.png"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(ADClick) forControlEvents:UIControlEventTouchUpInside];
        [ADView addSubview:btn];
        
    }
    
    // 为你优选  75
    CGFloat chooseViewH = 75.f/1334.f *UISCREEN_HEIGHT;
    UIView *chooseView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(ADView.frame), UISCREEN_WIDTH, chooseViewH)];
    chooseView.backgroundColor = YFColor_Background;
    //    chooseView.backgroundColor = [UIColor blueColor];
    
    [self addSubview:chooseView];
    {
        CGFloat tipLableW = 100;
        CGFloat tipLabelH = chooseViewH;
        UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tipLableW, tipLabelH)];
        tipLabel.center = CGPointMake(chooseView.mj_w/2, chooseViewH/2);
        tipLabel.text = @"为你优选";
        tipLabel.textAlignment = NSTextAlignmentCenter;
        tipLabel.textColor = YFColor_Text;
        tipLabel.font = [UIFont systemFontOfSize:13];
        [chooseView addSubview:tipLabel];
        
        CGFloat lineWidth = (chooseView.mj_w - tipLableW)/2;
        UIView *leftLine = [[UIView alloc] initWithFrame:CGRectMake(0, tipLabelH/2, lineWidth, lineW)];
        leftLine.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
        [chooseView addSubview:leftLine];
        
        UIView *rightLine = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(tipLabel.frame), tipLabelH/2, lineWidth, lineW)];
        rightLine.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
        [chooseView addSubview:rightLine];
    }
    // 一共 949
    self.height = CGRectGetMaxY(chooseView.frame);
    
    NSLog(@"height:%d",self.height);
}

// 线下入驻点击
- (void)registerUnderLineClick {
    if (self.registerUnderLineBlock) {
        self.registerUnderLineBlock();
    }
}

- (void)ADClick{
    YFShopRegisterVC *registerVC = [[YFShopRegisterVC alloc] init];
   
    UIViewController *vc = [self currentViewController];
    [vc.navigationController pushViewController:registerVC animated:YES];
}

//当前视图
-(UIViewController *)currentViewController{
    UIViewController *vc;
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[objc_getClass("UIViewController") class]] ) {
            vc=(UIViewController*)nextResponder;
            
            return vc;
        }
    }
    return vc;
}
#pragma mark - 代理方法
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{

    NSLog(@"%ld",index);
   HbannerModel *model =  [self.bannerArr objectAtIndex:index];
    if ([model.goods_id isEqualToString:@"0"]) {
        [MBPHUD showAlertWithTitle:@"该商品已下架"];
        return;
    }
    QJShopDetailVC *VC = [QJShopDetailVC new];
    VC.goodsID = [NSString stringWithoutNilFormart:model.goods_id];
    UIViewController *vc = [self currentViewController];
    [vc.navigationController pushViewController:VC animated:YES];
}

- (void)addGestureRecognizerTapWithImageView:(UIImageView *)imageView{
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doTap:)];
    // 允许用户交互
    imageView.userInteractionEnabled = YES;
    
    [imageView addGestureRecognizer:tap];
    
}
- (void)doTap:(UITapGestureRecognizer *)tap{
    
    NSString *goods_id = @"";
    if (tap.view.tag == 100) {
        HomeBigSmallModel *model = [self.bigArr objectAtIndex:0];
        goods_id = model.goods_id;
    }
    if (tap.view.tag == 200) {
        HomeBigSmallModel *model = [self.bigArr objectAtIndex:1];
        goods_id = model.goods_id;
    }
    if (tap.view.tag == 1000) {
        HomeBigSmallModel *model = [self.smallArr objectAtIndex:0];
        goods_id = model.goods_id;
    }
    if (tap.view.tag == 2000) {
        HomeBigSmallModel *model = [self.smallArr objectAtIndex:1];
        goods_id = model.goods_id;
    }
    
    if (tap.view.tag == 3000) {
        HomeBigSmallModel *model = [self.smallArr objectAtIndex:2];
        goods_id = model.goods_id;
    }
    
    if (tap.view.tag == 4000) {
        HomeBigSmallModel *model = [self.smallArr objectAtIndex:3];
        goods_id = model.goods_id;
    }
    
    if ([goods_id isEqualToString: @""]||[goods_id isEqualToString:@"0"]) {
        
        [MBPHUD showAlertWithTitle:@"该商品已下架"];
        return;
    }
    QJShopDetailVC *VC = [QJShopDetailVC new];
    VC.goodsID = [NSString stringWithoutNilFormart:goods_id];
    UIViewController *vc =  [self currentViewController];
    [vc.navigationController pushViewController:VC animated:YES];
}



ARRAY_INIT(bannerArr);
ARRAY_INIT(bigArr);
ARRAY_INIT(smallArr);
@end
